# 导入 socket、sys 模块
import socket
import threading

# 创建 socket 对象
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务器
host = socket.gethostname()
port = 9999
client_socket.connect((host, port))

def receive_messages():
    """接收来自服务器的消息"""
    while True:
        try:
            message = client_socket.recv(1024)
            if not message:
                break
            print(message.decode('utf-8'))
        except:
            print("与服务器断开连接")
            break

def send_messages():
    """发送消息到服务器"""
    while True:
        message = input("")
        client_socket.send(message.encode('utf-8'))

# 启动接收消息的线程
threading.Thread(target=receive_messages, daemon=True).start()

# 主线程用于发送消息
send_messages()